home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 2510.ZIP / TRSOURCE.EXE / SOUNDEX.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  4KB  |  131 lines

  1. /*********
  2. *  SOUNDEX
  3. *
  4. *  by Leonard Zerman and Tom Rettig
  5. *
  6. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  7. *
  8. *  Syntax: SOUNDEX( <expC> )
  9. *  Return: A code as a four char string in the form of A999. 
  10. *  Note  : This is based upon the Soundex algorithm in
  11. *          Donald E. Knuth from The Art of Computer Programming,
  12. *          Volume 3, "Sorting and Searching", page 392.
  13. *********/
  14.  
  15. #include "trlib.h"
  16.  
  17. TRTYPE soundex()                        /* declare the soundex function */
  18. {
  19.     char *instr;                    /* a pointer to the passed string */ 
  20.     static char funcname[] = {"soundex"};  /* program name for errors */                 
  21.     static char ret[5], hold[1];
  22.     int i; 
  23.  
  24.     if (PCOUNT==1 && ISCHAR(1) )    /* if there is one parameter and */
  25.     {                                        /* it is a char variable */                           
  26.         instr = _parc(1);              /* assign the address to instr */  
  27.     }
  28.     else                                     
  29.     {
  30.          _retc(_tr_errmsgs(funcname, E_SYNTAX));   /* return an error */      
  31.          return;                               /* return from program */     
  32.     }
  33.  
  34.     /* first char of string must be alpha ::= first char of soundex code */
  35.     if ( isalpha(*instr) )
  36.     {
  37.        ret[0] = toupper(*instr);
  38.        hold[0] = ret[0];
  39.     }
  40.     else
  41.     {                            /* return blank string to */
  42.        ret[0] = SPACEC;          /* place at top of index */
  43.        ret[1] = SPACEC;
  44.        ret[2] = SPACEC;
  45.        ret[3] = SPACEC;
  46.        ret[4] = NULLC;
  47.        _retc(ret);
  48.        return;
  49.     }
  50.  
  51.     i = 1;
  52.     instr++;
  53.     while (*instr && i <= 3)            
  54.     {
  55.        if ( hold[0] == toupper(*instr) )
  56.        {                            /* skip repeating characters */
  57.           instr++;
  58.           continue;
  59.        }  
  60.  
  61.        switch( *instr )             /* if it is one of these choices, */   
  62.        {                            /* put the proper code into ret[] */
  63.           case 'b':
  64.           case 'B':
  65.           case 'f':
  66.           case 'F':
  67.           case 'p':
  68.           case 'P':
  69.           case 'v':
  70.           case 'V':
  71.                hold[0]  = toupper(*instr);
  72.                ret[i++] = '1';
  73.                break;
  74.           case 'c':
  75.           case 'C':
  76.           case 'g':
  77.           case 'G':
  78.           case 'j':
  79.           case 'J':
  80.           case 'k':
  81.           case 'K':
  82.           case 'q':
  83.           case 'Q':
  84.           case 's':
  85.           case 'S':
  86.           case 'x':
  87.           case 'X':
  88.           case 'z':
  89.           case 'Z':
  90.                hold[0]  = toupper(*instr);
  91.                ret[i++] = '2';
  92.                break;
  93.           case 'd':
  94.           case 'D':
  95.           case 't':
  96.           case 'T':
  97.                hold[0]  = toupper(*instr);
  98.                ret[i++] = '3';
  99.                break;
  100.           case 'l':
  101.           case 'L':
  102.                hold[0]  = toupper(*instr);
  103.                ret[i++] = '4';
  104.                break;
  105.           case 'm':
  106.           case 'M':
  107.           case 'n':
  108.           case 'N':
  109.                hold[0]  = toupper(*instr);
  110.                ret[i++] = '5';
  111.                break;
  112.           case 'r':
  113.           case 'R':
  114.                hold[0]  = toupper(*instr);
  115.                ret[i++] = '6';
  116.                break;
  117.        }
  118.        instr++;    
  119.     }   
  120.  
  121.     /* if the soundex code is less than four, fill with '0' */
  122.     while (i <= 3)
  123.          ret[i++] = '0';
  124.  
  125.     ret[i] = NULLC;            /* null terminator */
  126.  
  127.    _retc(ret);                 /* return soundex string */
  128. }
  129. /* eof soundex */
  130.  
  131.